package com.bysc.commentservice.controller;


import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.commentservice.entity.CommentParent;
import com.bysc.commentservice.entity.vo.CommentFormVo;
import com.bysc.commentservice.service.CommentParentService;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-06-15
 */
@RestController
@Api(tags = "父评论管理")
@RequestMapping("/commentservice/comment-parent")
public class CommentParentController {


    @Autowired
    private CommentParentService commentParentService;

    @ApiOperation(value = "根据postId和Token新增父评论")
    @PostMapping("addParentCommentByPostIdAndToken")
    public Results addParentCommentByPostIdAndToken(@ApiParam(name = "parentCommentVo", value = "评论信息", required = true)
                                                    @RequestBody CommentFormVo parentCommentFormVo, HttpServletRequest httpRequest) {
        try {
            String userId = JwtUtils.getMemberIdByJwtToken(httpRequest);
            commentParentService.addParentCommentByPostIdAndToken(parentCommentFormVo, userId);
            return Results.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaiYunException(20001, "error");
        }
    }

    @ApiOperation(value = "根据postId分页查询最新父评论")
    @PostMapping("getCommentListByPostIdOrderBycreatTime/{id}/{page}/{limit}")
    public Results getCommentListByPostIdOrderBycreatTime(@ApiParam(name = "id", value = "帖子id", required = true)
                                                          @PathVariable String id, @ApiParam(name = "page", value = "查询页数", required = true)
                                                          @PathVariable Integer page, @ApiParam(name = "limit", value = "查询数量", required = true)
                                                          @PathVariable Integer limit) {
        try {
            List<CommentParent> list = commentParentService.getCommentListByPostIdOrderBycreatTime(id, page, limit);

            return Results.ok().data("commentList", list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaiYunException(20001, "error");
        }
    }

    @ApiOperation(value = "根据postId和Token删除父评论")
    @PostMapping("deleteParentCommentByPostIdAndToken/{id}")
    public Results deleteParentCommentByPostIdAndToken(@ApiParam(name = "parentCommentVo", value = "评论信息", required = true)
                                                       @RequestBody CommentFormVo parentCommentFormVo,
                                                       @PathVariable Object id,HttpServletRequest httpRequest) {
        try {
            String userId = JwtUtils.getMemberIdByJwtToken(httpRequest);
            // 未登录不能调用
            if (!StringUtils.isNotBlank(userId)){
                throw new BaiYunException(20002,"请先登录!");
            }
            commentParentService.deleteParentCommentByPostIdAndToken(parentCommentFormVo,id);
            return Results.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaiYunException(20001, "error");
        }
    }

    @ApiOperation(value = "根据postId和Token修改父评论")
    @PostMapping("updateParentCommentByPostIdAndToken/{id}")
    public Results updateParentCommentByPostIdAndToken(@ApiParam(name = "parentCommentVo", value = "评论信息", required = true)
                                                       @RequestBody CommentFormVo parentCommentFormVo,
                                                       @PathVariable Object id, HttpServletRequest httpRequest) {
        try {
            // 获取出用户id
            String userId = JwtUtils.getMemberIdByJwtToken(httpRequest);
            // 未登录不能调用
            if (!StringUtils.isNotBlank(userId)){
                throw new BaiYunException(20002,"请先登录!");
            }
            commentParentService.updateParentCommentByPostIdAndToken(parentCommentFormVo,id);
            return Results.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaiYunException(20001, "error");
        }


    }

}

